package com.huawei.hianalytics.f.g;

import android.content.Context;
import android.content.SharedPreferences;
import android.text.TextUtils;
import android.util.Pair;
import com.huawei.hianalytics.g.b;
import com.tencent.mm.opensdk.modelmsg.WXMediaMessage;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public final class a {
    private static a d;
    private String a;
    private String b;
    private Context c;
    private String e;

    private a(Context context) {
        this.c = context;
        this.e = context.getFilesDir().getPath();
    }

    public static a a(Context context) {
        a aVar;
        synchronized (a.class) {
            if (d == null) {
                d = new a(context);
            }
            aVar = d;
        }
        return aVar;
    }

    private String a(File file) {
        FileInputStream fileInputStream;
        String str;
        Throwable th;
        try {
            fileInputStream = new FileInputStream(file);
            try {
                com.huawei.hianalytics.util.a aVar = new com.huawei.hianalytics.util.a(WXMediaMessage.DESCRIPTION_LENGTH_LIMIT);
                byte[] bArr = new byte[WXMediaMessage.DESCRIPTION_LENGTH_LIMIT];
                while (true) {
                    int read = fileInputStream.read(bArr);
                    if (((long) read) == -1) {
                        break;
                    }
                    aVar.a(bArr, read);
                }
                if (aVar.a() == 0) {
                    str = "";
                    if (fileInputStream != null) {
                        try {
                            fileInputStream.close();
                        } catch (IOException e) {
                            b.c("HiAnalytics/event/aes128key", "inputStream Not closed!");
                        }
                    }
                } else {
                    str = new String(aVar.b(), "UTF-8");
                    if (fileInputStream != null) {
                        try {
                            fileInputStream.close();
                        } catch (IOException e2) {
                            b.c("HiAnalytics/event/aes128key", "inputStream Not closed!");
                        }
                    }
                }
            } catch (FileNotFoundException e3) {
                try {
                    b.c("HiAnalytics/event/aes128key", "getInfoFromFile(): No files need to be read");
                    str = "";
                    if (fileInputStream != null) {
                        try {
                            fileInputStream.close();
                        } catch (IOException e4) {
                            b.c("HiAnalytics/event/aes128key", "inputStream Not closed!");
                        }
                    }
                    return str;
                } catch (Throwable th2) {
                    th = th2;
                    if (fileInputStream != null) {
                        try {
                            fileInputStream.close();
                        } catch (IOException e5) {
                            b.c("HiAnalytics/event/aes128key", "inputStream Not closed!");
                        }
                    }
                    throw th;
                }
            } catch (IOException e6) {
                b.c("HiAnalytics/event/aes128key", "getInfoFromFile(): stream.read or new string exception");
                str = "";
                if (fileInputStream != null) {
                    try {
                        fileInputStream.close();
                    } catch (IOException e7) {
                        b.c("HiAnalytics/event/aes128key", "inputStream Not closed!");
                    }
                }
                return str;
            }
        } catch (FileNotFoundException e8) {
            fileInputStream = null;
            b.c("HiAnalytics/event/aes128key", "getInfoFromFile(): No files need to be read");
            str = "";
            if (fileInputStream != null) {
                fileInputStream.close();
            }
            return str;
        } catch (IOException e9) {
            fileInputStream = null;
            b.c("HiAnalytics/event/aes128key", "getInfoFromFile(): stream.read or new string exception");
            str = "";
            if (fileInputStream != null) {
                fileInputStream.close();
            }
            return str;
        } catch (Throwable th3) {
            Throwable th4 = th3;
            fileInputStream = null;
            th = th4;
            if (fileInputStream != null) {
                fileInputStream.close();
            }
            throw th;
        }
        return str;
    }

    private String a(File file, String str) {
        if (!file.exists()) {
            return "";
        }
        return a(new File(file.getPath(), "hianalytics_" + str));
    }

    private String a(String str, String str2) {
        String a = a(new File(f() + File.separator + str), str);
        File file;
        if (TextUtils.isEmpty(a)) {
            a = a(new File(f() + File.separator + "hianalytics" + File.separator + str2), str2);
            if (TextUtils.isEmpty(a)) {
                a = f.b();
                file = new File(f() + File.separator + "hianalytics" + File.separator + str2);
                if (!file.exists() && file.mkdirs()) {
                    b.c("HiAnalytics/event", "The secret key file creates the OK!");
                }
                b(new File(f() + File.separator + "hianalytics" + File.separator + str2, "hianalytics_" + str2), a);
            }
        } else {
            file = new File(f() + File.separator + str);
            e.a(file);
            if (file.isDirectory() && file.delete()) {
                b.c("HiAnalytics/event", "The secret key file is Directory del! change new file");
            }
            file = new File(f() + File.separator + "hianalytics" + File.separator + str2);
            if (!file.exists() && file.mkdirs()) {
                b.c("HiAnalytics/event", "The secret key file creates the OK!");
            }
            b(new File(f() + File.separator + "hianalytics" + File.separator + str2, "hianalytics_" + str2), a);
        }
        return a;
    }

    private void a(SharedPreferences sharedPreferences, String str) {
        byte[] a = d.a();
        long currentTimeMillis = System.currentTimeMillis();
        h.a(sharedPreferences, "PrivacyData", d.a(a, d.a(d(), a, str)));
        h.a(sharedPreferences, "flashKeyTime", Long.valueOf(currentTimeMillis));
    }

    public static String b() {
        String a = j.a();
        String a2 = com.huawei.hianalytics.f.a.a.a();
        return j.a(a + a2 + f.c(), 4);
    }

    private String b(Context context) {
        String str = (String) h.b(h.d(context, "analytics_key"), "analytics_key", "");
        if (TextUtils.isEmpty(str)) {
            SharedPreferences d = h.d(context, "Privacy_MY");
            str = (String) h.b(d, "PrivacyData", "");
            if (TextUtils.isEmpty(str)) {
                str = f.a();
                a(d, str);
                return str;
            }
            Pair a = d.a(str);
            return d.b(d(), (byte[]) a.first, (String) a.second);
        }
        a = d.a(str);
        Object b = d.b(d(), (byte[]) a.first, (String) a.second);
        if (!TextUtils.isEmpty(b)) {
            return b;
        }
        str = f.a();
        a(h.d(context, "Privacy_MY"), str);
        return str;
    }

    private void b(File file, String str) {
        Throwable th;
        FileOutputStream fileOutputStream = null;
        FileOutputStream fileOutputStream2;
        try {
            fileOutputStream2 = new FileOutputStream(file);
            try {
                fileOutputStream2.write(str.getBytes("UTF-8"));
                fileOutputStream2.flush();
                if (fileOutputStream2 != null) {
                    try {
                        fileOutputStream2.close();
                    } catch (IOException e) {
                        b.c("HiAnalytics/event/aes128key", "OutputStream not closed");
                    }
                }
            } catch (FileNotFoundException e2) {
                try {
                    b.c("HiAnalytics/event/aes128key", "saveInfoToFile(): No files need to be read");
                    if (fileOutputStream2 != null) {
                        try {
                            fileOutputStream2.close();
                        } catch (IOException e3) {
                            b.c("HiAnalytics/event/aes128key", "OutputStream not closed");
                        }
                    }
                } catch (Throwable th2) {
                    Throwable th3 = th2;
                    fileOutputStream = fileOutputStream2;
                    th = th3;
                    if (fileOutputStream != null) {
                        try {
                            fileOutputStream.close();
                        } catch (IOException e4) {
                            b.c("HiAnalytics/event/aes128key", "OutputStream not closed");
                        }
                    }
                    throw th;
                }
            } catch (IOException e5) {
                b.c("HiAnalytics/event/aes128key", "saveInfoToFile(): io exc from write info to file!");
                if (fileOutputStream2 != null) {
                    try {
                        fileOutputStream2.close();
                    } catch (IOException e6) {
                        b.c("HiAnalytics/event/aes128key", "OutputStream not closed");
                    }
                }
            }
        } catch (FileNotFoundException e7) {
            fileOutputStream2 = null;
            b.c("HiAnalytics/event/aes128key", "saveInfoToFile(): No files need to be read");
            if (fileOutputStream2 != null) {
                fileOutputStream2.close();
            }
        } catch (IOException e8) {
            fileOutputStream2 = null;
            b.c("HiAnalytics/event/aes128key", "saveInfoToFile(): io exc from write info to file!");
            if (fileOutputStream2 != null) {
                fileOutputStream2.close();
            }
        } catch (Throwable th4) {
            th = th4;
            if (fileOutputStream != null) {
                fileOutputStream.close();
            }
            throw th;
        }
    }

    private void c() {
        String str = f() + File.separator + "hianalytics" + File.separator + "aprpap";
        String str2 = f() + File.separator + "hianalytics" + File.separator + "febdoc";
        String str3 = f() + File.separator + "hianalytics" + File.separator + "marfil";
        String str4 = f() + File.separator + "hianalytics" + File.separator + "maywnj";
        b(new File(str, "hianalytics_aprpap"), f.b());
        b(new File(str2, "hianalytics_febdoc"), f.b());
        b(new File(str3, "hianalytics_marfil"), f.b());
        b(new File(str4, "hianalytics_maywnj"), f.b());
    }

    private String d() {
        if (TextUtils.isEmpty(this.a)) {
            this.a = e();
        }
        return this.a;
    }

    private String e() {
        return j.a(b(), a("secondAssembly", "aprpap"), a("thirdAssembly", "febdoc"), a("fourthAssembly", "marfil"), a("fiveAssembly", "maywnj"));
    }

    private String f() {
        return this.e;
    }

    public String a() {
        if (TextUtils.isEmpty(this.b)) {
            this.b = b(this.c);
        }
        return this.b;
    }

    public void a(String str, int i) {
        if (!TextUtils.isEmpty(str) && i == 1) {
            SharedPreferences d;
            long longValue = ((Long) h.b(h.d(this.c, "analytics_key"), "flashKeyTime", Long.valueOf(-1))).longValue();
            if (longValue == -1) {
                d = h.d(this.c, "Privacy_MY");
                longValue = ((Long) h.b(d, "flashKeyTime", Long.valueOf(-1))).longValue();
            } else {
                h.a(this.c, "../shared_prefs/", "analytics_key");
                d = h.d(this.c, "Privacy_MY");
                a(d, str);
                this.b = str;
            }
            if (System.currentTimeMillis() - longValue > 43200000) {
                this.b = str;
                longValue = ((Long) h.b(d, "assemblyFlash", Long.valueOf(-1))).longValue();
                if (longValue == -1) {
                    h.a(d, "assemblyFlash", Long.valueOf(System.currentTimeMillis()));
                } else if (System.currentTimeMillis() - longValue > 31536000000L) {
                    c();
                    h.a(d, "assemblyFlash", Long.valueOf(System.currentTimeMillis()));
                    this.a = e();
                }
                a(d, str);
            }
        }
    }
}
